#lang racket

;TAGGED-DATA-------------------------------------------------------------------------------------

(define (attach-tag type-tag contents)
  (cond ((number? contents) contents)
        (else (cons type-tag contents))))

(define (type-tag datum)
  (cond ((number? datum) 'scheme-number) 
        ((pair? datum) (car datum))
        (else (error "Incorrect tagged data: " datum))))

(define (contents datum)
  (cond ((number? datum) datum) 
        ((pair? datum) (cdr datum))
        (error "Incorrect tagged data: " datum)))

;HELP-PROCEDURES---------------------------------------------------------------------------------

(define (equal? x y)
  (cond ((and (not (pair? x)) (not (pair? y))) (eq? x y))
        ((and (pair? x) (pair? y)) (lists-equal? x y))
        (else false)))

(define (lists-equal? x y)
  (cond ((and (null? x) (null? y)) true)
        ((and (not (null? x)) (not (null? y))) (and
                                                (equal? (car x) (car y))
                                                (lists-equal? (cdr x) (cdr y))))
        (else false)))

(define (acc op initial seq)
  (if (null? seq)
      initial
      (op (car seq)
          (acc op initial (cdr seq)))))

(define (same-elements? xs)
  (if (null? xs)
      true
      (acc (lambda (x y) (and x y))
           true
           (map (lambda (x) (equal? x (car xs)))
                xs))))

(define (zip f list1 list2)
  (if (or (null? list1) (null? list2))
      '()
      (cons (f (car list1) (car list2))
            (zip f (cdr list1) (cdr list2)))))

(define (id x) x)

;OP-ENTRY----------------------------------------------------------------------------------------

(define (make-op-entry op type element)
  (list op type element))

(define (this-op? op type entry)
  (and (equal? (car entry) op) (equal? (cadr entry) type)))

(define (get-op-entry entry)
  (caddr entry))

;OPERATION-TABLE---------------------------------------------------------------------------------

(define op-table '())

(define (put op type element)
  (let ((new-entry (make-op-entry op type element)))
    (set! op-table (cons new-entry op-table)))
  op-table)

(define (get op type)
  (define (get-operation op type operation-table)
    (cond ((null? operation-table) false)
          ((this-op? op type (car operation-table)) (get-op-entry (car operation-table)))
          (else (get-operation op type (cdr operation-table)))))
  (get-operation op type op-table))

;APPLY-GENERIC-----------------------------------------------------------------------------------

(define (apply-generic op . args)
  (let ((type-tags (map type-tag args)))
    (let ((proc (get op type-tags)))
      (if proc
          (apply proc (map contents args))
          (let ((coerce-try (try-coercions op type-tags type-tags)))
            (if coerce-try
                (let ((proc (car coerce-try))
                      (coercions (cadr coerce-try)))
                  (let ((coerced-args (zip
                                       (lambda (x y) (apply x (list y)))
                                       coercions
                                       args)))
                    (apply proc (map contents coerced-args))))
                (error "no coercions found: " (list op args))))))))

(define (try-coercions op type-tags coerce-types)
  (define (recur remaining-coerce-types)
    (if (null? remaining-coerce-types)
        false
        (let ((coerce-try (try-coerce op type-tags (car remaining-coerce-types))))
          (if coerce-try
              coerce-try
              (recur (cdr remaining-coerce-types))))))
  (recur coerce-types))

(define (try-coerce op type-tags coerce-type)
  (let ((coerced-op (coerce-op op type-tags coerce-type))
        (coercions (coerce-types type-tags coerce-type)))
    (if (and coerced-op coercions)
        (list coerced-op coercions)
        false)))

(define (coerce-op op type-tags coerce-type)
  (let ((coerced-types (map
                        (lambda (x) coerce-type)
                        type-tags)))
    (get op coerced-types)))

(define (coerce-types type-tags coerce-type)
  (let ((coercions (map
                    (lambda (x) (get-coercion x coerce-type))
                    type-tags)))
    (let ((failed-coercions (filter
                             (lambda (x) (eq? x false))
                             coercions)))
      (if (null? failed-coercions)
          coercions
          false))))

;COERCION-TABLE----------------------------------------------------------------------------------

(define coercion-table '())

(define (put-coercion type1 type2 coercion)
  (let ((new-coercion (make-op-entry type1 type2 coercion)))
    (set! coercion-table (cons new-coercion coercion-table)))
  coercion-table)

(define (get-coercion type1 type2)
  (define (recur coer-table)
    (cond ((null? coer-table) false)
          ((this-op? type1 type2 (car coer-table)) (get-op-entry (car coer-table)))
          (else (recur (cdr coer-table)))))
  (if (eq? type1 type2)
      id
      (recur coercion-table)))

;PACKAGE-DEFINITIONS-----------------------------------------------------------------------------

(define (install-rectangular-package)
  (define (square x) (* x x))
  (define (real-part z) (car z))
  (define (imag-part z) (cdr z))
  (define (make-from-real-imag x y) (cons x y))
  (define (magnitude z)
    (sqrt (+ (square (real-part z))
             (square (imag-part z)))))
  (define (angle z)
    (atan (imag-part z) (real-part z)))
  (define (make-from-mag-ang r a)
    (cons (* r (cos a)) (* r (sin a))))
  (define (equ-rectangular? z1 z2)
    (and (equ? (real-part z1) (real-part z2))
         (equ? (imag-part z1) (imag-part z2))))
  (define (=zero-rectangular? z1)
    (and (= (real-part z1) 0) (= (imag-part z1) 0)))
  
  (define (tag x) (attach-tag 'rectangular x))
  (put 'real-part '(rectangular) real-part)
  (put 'imag-part '(rectangular) imag-part)
  (put 'magnitude '(rectangular) magnitude)
  (put 'angle '(rectangular) angle)
  (put 'make-from-real-imag 'rectangular
       (lambda (x y) (tag (make-from-real-imag x y))))
  (put 'make-from-mag-ang 'rectangular
       (lambda (r a) (tag (make-from-mag-ang r a))))
  (put 'equ? '(rectangular rectangular) equ-rectangular?)
  (put '=zero? '(rectangular) =zero-rectangular?)
  'done)

(define (install-polar-package)
  (define (square x) (* x x))
  (define (magnitude z) (car z))
  (define (angle z) (cdr))
  (define (make-from-mag-ang r a) (cons r a))
  (define (real-part z)
    (* (magnitude z) (cos (angle z))))
  (define (imag-part z)
    (* (magnitude z) (sin (angle z))))
  (define (make-from-real-imag x y)
    (cons (sqrt (+ (square x) (square y)))
          (atan y x)))
  (define (equ-polar? z1 z2)
    (and (equ? (magnitude z1) (magnitude z2))
         (equ? (angle z1) (angle z2))))
  (define (=zero-polar? z1)
    (and (= (magnitude z1) 0) (= (angle z1) 0)))
  
  (define (tag x) (attach-tag 'polar x))
  (put 'real-part '(polar) real-part)
  (put 'imag-part '(polar) imag-part)
  (put 'magnitude '(polar) magnitude)
  (put 'angle '(polar) angle)
  (put 'make-from-real-imag 'polar
       (lambda (x y) (tag (make-from-real-imag x y))))
  (put 'make-from-mag-ang 'polar
       (lambda (r a) (tag (make-from-mag-ang r a))))
  (put 'equ? '(polar polar) equ-polar?)
  (put '=zero? '(polar) =zero-polar?)
  'done)

(define (install-scheme-number-package)
  (define (tag x)
    (attach-tag 'scheme-number x))
  (put 'add '(scheme-number scheme-number)
       (lambda (x y) (tag (+ x y))))
  (put 'sub '(scheme-number scheme-number)
       (lambda (x y) (tag (- x y))))
  (put 'mul '(scheme-number scheme-number)
       (lambda (x y) (tag (* x y))))
  (put 'div '(scheme-number scheme-number)
       (lambda (x y) (tag (/ x y))))
  (put 'make 'scheme-number
       (lambda (x) (tag x)))
  (put 'equ? '(scheme-number scheme-number) =)
  (put 'zero? '(scheme-number)
       (lambda (x) (= x 0)))
  'done)

(define (install-rational-package)
  (define (numer x) (car x))
  (define (denom x) (cdr x))
  (define (make-rat n d)
    (let ((g (gcd n d)))
      (cons (/ n g) (/ d g))))
  (define (add-rat x y)
    (make-rat (+ (* (numer x) (denom y))
                 (* (numer y) (denom x)))
              (* (denom x) (denom y))))
  (define (sub-rat x y)
    (make-rat (- (* (numer x) (denom y))
                 (* (numer y) (denom x)))
              (* (denom x) (denom y))))
  (define (mul-rat x y)
    (make-rat (* (numer x) (numer y))
              (* (denom x) (denom y))))
  (define (div-rat x y)
    (make-rat (* (numer x) (denom y))
              (* (denom x) (numer y))))
  (define (equ-rational? x y)
    (and (equ? (numer x) (numer y))
         (equ? (denom x) (denom y))))
  (define (=zero-rational? r)
    (and (= (numer r) 0) (= (denom r) 0)))
  
  (define (tag x) (attach-tag 'rational x))
  (put 'add '(rational rational)
       (lambda (x y) (tag (add-rat x y))))
  (put 'sub '(rational rational)
       (lambda (x y) (tag (sub-rat x y))))
  (put 'mul '(rational rational)
       (lambda (x y) (tag (mul-rat x y))))
  (put 'div '(rational rational)
       (lambda (x y) (tag (div-rat x y))))
  (put 'make 'rational
       (lambda (n d) (tag (make-rat n d))))
  (put 'equ? '(rational rational) equ-rational?)
  (put '=zero? '(rational) =zero-rational?)
  'done)

(define (install-complex-package)
  (define (make-from-real-imag x y)
    ((get 'make-from-real-imag 'rectangular) x y))
  (define (make-from-mag-ang r a)
    ((get 'make-from-mag-ang 'polar) r a))
  
  (define (add-complex z1 z2)
    (make-from-real-imag (+ (real-part z1) (real-part z2))
                         (+ (imag-part z1) (imag-part z2))))
  (define (sub-complex z1 z2)
    (make-from-real-imag (- (real-part z1) (real-part z2))
                         (- (imag-part z1) (imag-part z2))))
  (define (mul-complex z1 z2)
    (make-from-mag-ang (* (magnitude z1) (magnitude z2))
                       (+ (angle z1) (angle z2))))
  (define (div-complex z1 z2)
    (make-from-mag-ang (/ (magnitude z1) (magnitude z2))
                       (- (angle z1) (angle z2))))
  
  (define (tag z) (attach-tag 'complex z))
  (put 'add '(complex complex)
       (lambda (z1 z2) (tag (add-complex z1 z2))))
  (put 'sub '(complex complex)
       (lambda (z1 z2) (tag (sub-complex z1 z2))))
  (put 'mul '(complex complex)
       (lambda (z1 z2) (tag (mul-complex z1 z2))))
  (put 'div '(complex complex)
       (lambda (z1 z2) (tag (div-complex z1 z2))))
  (put 'make-from-real-imag 'complex
       (lambda (x y) (tag (make-from-real-imag x y))))
  (put 'make-from-mag-ang 'complex
       (lambda (r a) (tag (make-from-mag-ang r a))))
  (put 'real-part '(complex) real-part)
  (put 'imag-part '(complex) imag-part)
  (put 'magnitude '(complex) magnitude)
  (put 'angle '(complex) angle)
  (put 'equ? '(complex complex) equ?)
  (put '=zero? '(complex) =zero?)
  'done)

;COERION-OPERATIONS-----------------------------------------------------------------------------

(define (install-coercion-package)
  (define (scheme-number->rational n)
    (make-rational n 1))
  (define (scheme-number->complex n)
    (make-complex-from-real-imag n 0))
  (define (rational->complex r)
    (make-complex-from-real-imag (/ r 1.0) 0))
  
  (put-coercion 'scheme-number 'rational
                scheme-number->rational)
  (put-coercion 'scheme-number 'complex
                scheme-number->complex)
  (put-coercion 'rational 'complex
                rational->complex)
  'done)

;GENERIC-OPERATIONS-----------------------------------------------------------------------------

(define (real-part z) (apply-generic 'real-part z))
(define (imag-part z) (apply-generic 'imag-part z))
(define (magnitude z) (apply-generic 'magnitude z))
(define (angle z) (apply-generic 'angle z))

(define (make-from-real-imag x y)
  ((get 'make-from-real-imag 'rectangular) x y))
(define (make-from-mag-ang r a)
  ((get 'make-from-mag-ang 'polar) r a))

(define (add x y) (apply-generic 'add x y))
(define (sub x y) (apply-generic 'sub x y))
(define (mul x y) (apply-generic 'mul x y))
(define (div x y) (apply-generic 'div x y))

(define (make-scheme-number n)
  ((get 'make 'scheme-number) n))

(define (make-rational n d)
  ((get 'make 'rational) n d))

(define (make-complex-from-real-imag x y)
  ((get 'make-from-real-imag 'complex) x y))

(define (make-complex-from-mag-ang r a)
  ((get 'make-from-mag-ang 'complex) r a))

(define (equ? n1 n2) (apply-generic 'equ? n1 n2))

(define (=zero? n) (apply-generic '=zero? n))

;INSTALLING-PACKAGES-----------------------------------------------------------------------------

(install-rectangular-package)
(install-polar-package)
(install-scheme-number-package)
(install-rational-package)
(install-complex-package)

(install-coercion-package)

;TESTING-----------------------------------------------------------------------------------------

(define z1 (make-complex-from-real-imag 1 2))
(define z2 (make-complex-from-real-imag 30 40))

(mul z1 10)